package com.xiaohe.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xiaohe.gulimall.product.dao.BrandDao;
import com.xiaohe.gulimall.product.dao.CategoryDao;
import com.xiaohe.gulimall.product.entity.BrandEntity;
import com.xiaohe.gulimall.product.entity.CategoryEntity;
import com.xiaohe.gulimall.product.service.BrandService;
import com.xiaohe.gulimall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaohe.common.utils.PageUtils;
import com.xiaohe.common.utils.Query;

import com.xiaohe.gulimall.product.dao.CategoryBrandRelationDao;
import com.xiaohe.gulimall.product.entity.CategoryBrandRelationEntity;
import com.xiaohe.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandDao brandDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private BrandService brandService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        //品牌id
        Long brandId = categoryBrandRelation.getBrandId();
        //分类id
        Long catelogId = categoryBrandRelation.getCatelogId();
        //查询详细的名字
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);

        //获取品牌名字和分类名字
        String brandName = brandEntity.getName();
        String cateName = categoryEntity.getName();
        categoryBrandRelation.setBrandName(brandName);
        categoryBrandRelation.setCatelogName(cateName);
        //保存
        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrand(String name, Long brandId) {
        //更新关联表
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);
        //更新条件
        UpdateWrapper<CategoryBrandRelationEntity> wrapper = new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId);
        this.update(categoryBrandRelationEntity,wrapper);
    }

    @Override
    public void updateCategory(Long catId, String name) {
        categoryDao.updateCategory(catId,name);
    }

    @Override
    public List<BrandEntity> getBrandsByCatIds(Long catId) {
        QueryWrapper<CategoryBrandRelationEntity> categoryBrandRelationEntityQueryWrapper = new QueryWrapper<>();
        categoryBrandRelationEntityQueryWrapper.eq("catelog_id",catId);
        List<CategoryBrandRelationEntity> list = this.list(categoryBrandRelationEntityQueryWrapper);
        List<BrandEntity> collect = list.stream().map(item -> {
            Long brandId = item.getBrandId();
            BrandEntity byId = brandService.getById(brandId);
            return byId;
        }).collect(Collectors.toList());
        return collect;
    }


}